#include "WindowManager.h"
#include "GameConfiguration.h"
#include "Logger.h"

extern Logger* logger;
extern SDL_Renderer* gRenderer;
SDL_Renderer* WindowManager::srenderer = NULL;

WindowManager::WindowManager()
{
	window = NULL;
	renderer = NULL;
	logger->info("WindowManager-Created");
}


WindowManager::~WindowManager()
{
}

bool WindowManager::SetupWindow()
{
	logger->info("SetupWindow:INIT");

	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
	{
		logger->info("SDL could not initialize");
	}
	
	window = SDL_CreateWindow(
				"Ninja",
				SDL_WINDOWPOS_UNDEFINED, 
				SDL_WINDOWPOS_UNDEFINED,
				gameconfig::SCREEN_WIDTH,
				gameconfig::SCREEN_HEIGHT,
				SDL_WINDOW_SHOWN
	);

	if (window == NULL)
	{
		logger->info("Window Creation: FAILED");
		return false;
	}
	_screenSurface = SDL_GetWindowSurface(window);

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (renderer == NULL)
	{
		logger->info("Unable to create RENDERER");
		return false;
	}

	srenderer = renderer;
	gRenderer = renderer;

	SetClearColor();
	return true;
}


void WindowManager::SetClearColor()
{
	SDL_SetRenderDrawColor(renderer, 51, 164, 232, 255);
}

void WindowManager::DestroyWindow()
{
	SDL_FreeSurface(_screenSurface);
	_screenSurface = NULL;

	SDL_DestroyWindow(window);
	window = NULL;

	SDL_DestroyRenderer(renderer);
	SDL_Quit();
}

SDL_Renderer* WindowManager::GetRenderer()
{
	return renderer;
}

SDL_Renderer* WindowManager::GetSRenderer()
{
	return srenderer;
}